package cn.iocoder.yudao.hires.controller.admin;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.hires.entity.DictSpecializedDO;
import cn.iocoder.yudao.hires.service.DictSpecializedService;
import cn.iocoder.yudao.hires.vo.DictSpecializedPageReqVO;
import cn.iocoder.yudao.hires.vo.DictSpecializedRespVO;
import cn.iocoder.yudao.hires.vo.DictSpecializedSaveReqVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 专业字典")
@RestController
@RequestMapping("/resume/dict-specialized")
@Validated
public class DictSpecializedController {

    @Resource
    private DictSpecializedService dictSpecializedService;

    @PostMapping("/create")
    @Operation(summary = "创建专业字典")
    @TenantIgnore
    @PermitAll
    public CommonResult<Integer> createDictSpecialized(@Valid @RequestBody DictSpecializedSaveReqVO createReqVO) {
        return success(dictSpecializedService.createDictSpecialized(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新专业字典")
    @TenantIgnore
    @PermitAll
    public CommonResult<Boolean> updateDictSpecialized(@Valid @RequestBody DictSpecializedSaveReqVO updateReqVO) {
        dictSpecializedService.updateDictSpecialized(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除专业字典")
    @TenantIgnore
    @PermitAll
    public CommonResult<Boolean> deleteDictSpecialized(@RequestParam("id") Integer id) {
        dictSpecializedService.deleteDictSpecialized(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得专业字典")
    @TenantIgnore
    @PermitAll
    public CommonResult<DictSpecializedRespVO> getDictSpecialized(@RequestParam("id") Integer id) {
        DictSpecializedDO dictSpecialized = dictSpecializedService.getDictSpecialized(id);
        return success(BeanUtils.toBean(dictSpecialized, DictSpecializedRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得专业字典分页")
    @TenantIgnore
    @PermitAll
    public CommonResult<PageResult<DictSpecializedRespVO>> getDictSpecializedPage(@Valid DictSpecializedPageReqVO pageReqVO) {
        PageResult<DictSpecializedDO> pageResult = dictSpecializedService.getDictSpecializedPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DictSpecializedRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出专业字典 Excel")
    @TenantIgnore
    @PermitAll
    @ApiAccessLog(operateType = EXPORT)
    public void exportDictSpecializedExcel(@Valid DictSpecializedPageReqVO pageReqVO,
                                           HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DictSpecializedDO> list = dictSpecializedService.getDictSpecializedPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "专业字典.xls", "数据", DictSpecializedRespVO.class,
                BeanUtils.toBean(list, DictSpecializedRespVO.class));
    }

}